package org.springboot.szsrcb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Req_base_info;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.Req_team_info_log;
import org.springboot.szsrcb.entity.Req_work_log;
import org.springboot.szsrcb.entity.Req_workload_info;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.Req_bill_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/req")
public class ReqRestController_Export {

	private static final Logger logger = LoggerFactory.getLogger(ReqRestController_Export.class);

	@Autowired
	private Req_bill_Service req_bill_Service;
	
	@Autowired
	private UserService userService;
	
		
	@RequestMapping(value = "/req_trace_grid", method = { RequestMethod.GET })
	public String req_trace_grid(HttpServletRequest request) {
		logger.info("access /req/req_trace_grid");		
	
		String s_tmp=trans_2_json_req_trace_grid();
		logger.info("req_modify_detail_team_info_grid :" + s_tmp);
		return s_tmp;	
		
	}


	private String trans_2_json_req_trace_grid() {
		List<Req_bill> all_req_list=req_bill_Service.search_all_req_project();
				String r_start="{rows:[";		
				String r_end="]}";
				String r_content="";
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
				int count_value=0;
				for (Req_bill req_bill : all_req_list) {
					count_value=count_value+1;
					String tmp_start="{ id:"+(count_value)+",data:[";
					String tmp_end="]}";
					String tmp_content="";
					if(count_value < all_req_list.size()){
						tmp_end=tmp_end+",";
					}
										
					//ID
					tmp_content="'"+req_bill.getId()+"',";  
					//String tmp_content="";
					//项目主题
					tmp_content=tmp_content+"'"+req_bill.getReq_title()+"',";

					Req_base_info req_base_inf= req_bill_Service.search_req_base_info(String.valueOf(req_bill.getId()));
					if (req_base_inf != null) {
						//已分配 已完成 状态
						//填报人
						String str_reporter="";
						String str_role="";
						String status1="已接收";
						String status2="已分配";
						String status3="已完成";
						String get_req_status=req_bill.getReq_status();
						if(get_req_status.equals(status1)){
							//业务方案管理中心负责人
							str_reporter=userService.find_business_manager();
							str_role="负责人";
						}
						if(get_req_status.equals(status2)||get_req_status.equals(status3)){
							///需求负责人、需求分析人、需求监理人
							str_reporter=req_base_inf.getReq_head()+"/"+req_base_inf.getReq_analyst()+"/"+req_base_inf.getReq_supervisor();
							str_role="需求负责人/成员/监理人";
						}
						tmp_content=tmp_content+"'"+str_reporter+"',";
						//项目角色
						tmp_content=tmp_content+"'"+str_role+"',";
						//Req_workload_info obj=req_bill_Service.search_a_req_workload_info(req_id,s_array[i],role);
						//类型
						tmp_content=tmp_content+"'"+req_base_inf.getReq_difficult()+"',";
						//领取时间
						String date_tmp="";
						
							if (req_base_inf.getCreate_time()!=null) {								
								date_tmp= sdf2.format(req_base_inf.getCreate_time());	
							} 
					
						
						tmp_content=tmp_content+"'"+date_tmp+"',";
						//成员 
						tmp_content=tmp_content+"'"+req_base_inf.getReq_analyst()+"',";
						//监理
						tmp_content=tmp_content+"'"+req_base_inf.getReq_supervisor()+"',";
						//技术负责人
						tmp_content=tmp_content+"'"+req_base_inf.getTechnical_director()+"',";
						//项目状态
						String tmp_status="领取需求";
						String status_value1="已完成";
						
						if(get_req_status.equals(status_value1)){
							tmp_status="提交需求";
						}
						tmp_content=tmp_content+"'"+tmp_status+"',";
						//项目节点
						tmp_content=tmp_content+"'"+req_base_inf.getNode_value()+"',";
						//项目日志/周报
						 List<Req_work_log> log_list=req_bill_Service.search_req_work_log(String.valueOf(req_bill.getId()));
						 String log_tmp="'";
						 if(log_list!=null){
							for (Req_work_log req_work_log : log_list) {
								if (log_tmp.length()<2) {
									log_tmp=log_tmp+sdf2.format(req_work_log.getWork_date())+" "+req_work_log.getUser_name()+"  "+req_work_log.getWork_log();
								} else {
									log_tmp=log_tmp+"<br>"+sdf2.format(req_work_log.getWork_date())+" "+req_work_log.getUser_name()+"  "+req_work_log.getWork_log();
								}
								
							} 
						 }
						tmp_content=tmp_content+log_tmp+"'";
						tmp_content=tmp_start+tmp_content+tmp_end;		
						r_content=r_content+tmp_content;
					}else{
						//已接收，未分配状态时
						String str_reporter="";
						String str_role="";
						
						str_reporter=userService.find_business_manager();
						
						str_role="负责人";
						
						tmp_content=tmp_content+"'"+str_reporter+"',";
						//项目角色
						tmp_content=tmp_content+"'"+str_role+"',";
						//Req_workload_info obj=req_bill_Service.search_a_req_workload_info(req_id,s_array[i],role);
						//类型
						tmp_content=tmp_content+"'',";
						//领取时间
						String date_tmp="";
					
							if (req_bill.getAccept_date()!=null) {								
								date_tmp= sdf2.format(req_bill.getAccept_date());	
							} 
						
						
						tmp_content=tmp_content+"'"+date_tmp+"',";
						//成员 
						tmp_content=tmp_content+"'无',";
						//监理
						tmp_content=tmp_content+"'无',";
						//技术负责人
						tmp_content=tmp_content+"'无',";
						//项目状态												
						tmp_content=tmp_content+"'领取需求',";
						//项目节点
						tmp_content=tmp_content+"'1.领取需求',";
						//项目日志/周报
						tmp_content=tmp_content+"'',";
						tmp_content=tmp_start+tmp_content+tmp_end;		
						r_content=r_content+tmp_content;
					}
						
				}
				return r_start+r_content+r_end;
	}	
	
	private Map<String, Object> trans_json_value(Map<String, Object> map){
		String return_v="";
		String tmp_content="";
		
		int count_value=(int)map.get("count_value");
		String req_id=(String)map.get("req_id");
		String input_value=(String)map.get("input_value");
		//Req_base_info req_base_info=(Req_base_info)map.get("req_base_info");
		String role=(String)map.get("role");
		if (input_value==null||input_value.equals("")) {
			tmp_content="";
		} else {
			String[] s_array=input_value.split(",");
			for (int i = 0; i < s_array.length; i++) {				
					String tmp_start="{ id:"+(count_value)+",data:[";
					String tmp_end="]}";
					if((i+1)<s_array.length){
						tmp_end=tmp_end+",";
					}
					
					//ID
					tmp_content="'"+count_value+"',";  
					//String tmp_content="";
					//name
					tmp_content=tmp_content+"'"+s_array[i]+"',";
					//角色
					tmp_content=tmp_content+"'"+role+"',";
					//计划完成日	
					Req_workload_info obj=req_bill_Service.search_a_req_workload_info(req_id,s_array[i],role);
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
					if(obj==null){
						//计划完成日	
						tmp_content=tmp_content+"'',";
						//实际完成日
						tmp_content=tmp_content+"'',";	
						//计划工作量
						tmp_content=tmp_content+"'',";	
						//实际工作量
						tmp_content=tmp_content+"''";							
					}else{
											
						//计划完成日	
						String date_tmp="";
						if (obj.getPlan_complete_date()!=null) {
							date_tmp= sdf.format(obj.getPlan_complete_date());	
						} 
						tmp_content=tmp_content+"'"+date_tmp+"',";
						//实际完成日
						if (obj.getActual_complete_date()!=null) {
							date_tmp= sdf.format(obj.getActual_complete_date());	
						} 		
							
						tmp_content=tmp_content+"'"+date_tmp+"',";
						//计划工作量
						//if (obj.getPlan_workload()==null) {
						//	tmp_content=tmp_content+"'0',";
						//} else {
							tmp_content=tmp_content+"'"+obj.getPlan_workload()+"',";
						//}
						
						//实际工作量
						//if (obj.getActual_workload()==null) {
						//	tmp_content=tmp_content+"'0'";
						//} else {
							tmp_content=tmp_content+"'"+obj.getActual_workload()+"'";
						//}						
					}												
					tmp_content=tmp_start+tmp_content+tmp_end;		
					return_v=return_v+tmp_content;
					count_value=count_value+1;
			}
		}
		//return_v=tmp_content;
		Map<String, Object> map_r = new HashMap<String, Object>();
		map_r.put("json_value", return_v);
		map_r.put("count_value", count_value);				
		return map_r;
		
	}
}
